// //////////////////// Don't change anything after ! //////////////////////////

var IDEDebug = true;

function LOGD(msg) {
	if (IDEDebug) {
		console.log("[IDE DEBUG]:" + msg);
	}
}

function LOGI(msg) {
	console.log("[IDE INFO]:" + msg);
}

function LOGE(msg) {
	console.log("[IDE ERROR]:" + msg);
}

function send(message) {
	if (IDE.ws != null) {
		IDE.ws.send(message);
	} else {
		IDE.msg[IDE.msg.length] = message;
		IDE.onload();
	}
}

var ws = null;
var IDE = {
	onload : function() {
		IDE.connect();
		Init.initSingleTonOverLay();
		Init.initPredefineSize();
		Init.initButtonMouseAnimation();
		Init.initKey();
	},
	ws : null,
	msg : [],
	isConnecting : false,
	connect : function() {
		if (IDE.isConnecting || IDE.ws != null) {
			return;
		}
		IDE.isConnecting = true;
		var target = 'ws://' + window.location.host + '/' + SysCfg.projectName
				+ '/logMessage';
		if ('WebSocket' in window) {
			IDE.ws = new WebSocket(target);
		} else if ('MozWebSocket' in window) {
			IDE.ws = new MozWebSocket(target);
		} else {
			return;
		}
		IDE.ws.onopen = function() {
			setConnected(true);
			IDE.isConnecting = false;
			IDE.isUserClose = false;
			Init.initCmd();
			Init.initUserList();
			Init.initPluginList();
			Init.initServiceList();
			Init.initCerList();
			Init.getPluginPath();
			LOGD('Info: WebSocket connection opened.');
			for (var i = 0; i < IDE.msg.length; i++) {
				send(IDE.msg[i]);
			}
		};
		IDE.ws.onmessage = function(event) {
			log(event.data);
		};
		IDE.ws.onclose = function() {
			LOGD('Info: WebSocket connection closed.');
			IDE.isConnecting = false;
			IDE.ws = null;
			IDE.onclose();
		};
	},
	disconnect : function() {
		if (IDE.ws != null) {
			IDE.ws.close();
			IDE.ws = null;
		}
		setConnected(false);
	},
	onclose : function() {
		Init.ideClose();
	}
};

function setConnected(isConnected) {
	LOGD("IDE isConnected: " + isConnected);
}

function isNumber(s) {
	try {
		var regu = "^[0-9]+$";
		var re = new RegExp(regu);
		if (s.search(re) != -1) {
			return true;
		} else {
			return false;
		}
	} catch (e) {
		LOGE(e);
	}

}

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}
